﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using MeTalker.Aide.data;

namespace MeTalker.Aide.Forms
{
    internal partial class frmImportChatLog : MeForm
    {
        string account = string.Empty;

        public frmImportChatLog()
        {
            InitializeComponent();
            progressBar1.Maximum = 100;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void btnImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            open.FileName = "";
            open.Filter = "*.txt|*.txt";
            open.ShowDialog();
            string fileName = open.FileName;
            
            if (!string.IsNullOrEmpty(fileName))
            {
                int count = 0;
                using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    StreamReader reader = new StreamReader(fileStream,Encoding.Default);
                    account = reader.ReadLine();
                    string msg = reader.ReadLine();
                    bool readbegin = false;
                    
                    string answer = string.Empty;
                    string problem = string.Empty;
                    string new1;
                    string new2;
                    bool flag = true;
                    while ((msg = reader.ReadLine())!=null)
                    {
                        string[] msgs = msg.Split(':');
                       // Console.WriteLine(msg);
                        if (msgs.Length >= 2)
                        {
                            //if (!readbegin)
                            //{
                            //    if (!msg.Contains(account))
                            //    {
                                    readbegin = true;
                            //    }
                            //}
                        }
                        if (readbegin)
                        {
                            int index = msg.LastIndexOf(":");
                            string tempStr = msg.Substring(index + 1, msg.Length - (index + 1));
                            //if (msg.Contains(account))
                            if (flag)
                            {
                                flag = false;
                                answer = tempStr.Trim();
                            }
                            else
                            {
                                flag = true;
                                problem = tempStr.Trim();
                            }
                            
                            if (!string.IsNullOrEmpty(answer) && !string.IsNullOrEmpty(problem)) {

                                var ds = from temp in DataFactory.ChatRecords.Records where temp.Text.Equals(answer) select temp;
                                if (ds.Count() <= 0)
                                {
                                    count++;
                                    Console.WriteLine("导入一条数据......." + count);
                                    
                                    DataFactory.ChatRecords.Records.Add(new Record() { Time="", DateType = DataType.Log, Words = UtilHelper.SplitMsg(problem), Text = answer });
                                    answer = string.Empty;
                                    problem = string.Empty;
                                }
                                else {
                                    foreach (var d in ds)
                                    {
                                        d.Words += "," + problem;
                                    }
                                    answer = string.Empty;
                                    problem = string.Empty;
                                }
                            }
                            //Console.WriteLine(index);
                        }
                    }
                }
                progressBar1.Value = 100;
                MeMessageBox.ShowInformation(string.Format("导入旺旺聊天日志成功, 导入{0}条数据", count));
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            UtilHelper.RunBrowser(UtilHelper.HELP_BASE_URL + "59.html");
        }
    }
}
